package models

import (
	"gorm.io/gorm"
)

// 用户表
type User struct {
	gorm.Model
	Mobile          string `gorm:"type:varchar(11);comment:'手机号'"`
	Password        string `gorm:"type:varchar(100);comment:'密码'"`
	Nickname        string `gorm:"type:varchar(30);comment:'昵称'"`
	MemberStatus    int    `gorm:"type:tinyint;default:1;comment:'会员状态 1:普通 2:会员'"`
	MemberExpiredAt string `gorm:"varchar(30);comment:'会员有效期'"`
}

func (u *User) GetUserByMobile(db *gorm.DB, mobile string) error {
	return db.Where("mobile=?", mobile).Limit(1).Find(&u).Error
}

func (u *User) GetUserById(db *gorm.DB, id int) error {
	return db.Where("id = ?", id).Limit(1).Find(&u).Error
}

func (u *User) Register(db *gorm.DB) error {
	return db.Create(&u).Error
}

// 视频剧集播放记录
type PlayRecord struct {
	gorm.Model
	VideoId   int64  `gorm:"type:int;not null;comment:视频ID"`
	EpisodeId int64  `gorm:"type:int;not null;comment:集数ID"`
	UserId    int64  `gorm:"type:int;not null;comment:用户ID"`
	Seconds   string `gorm:"type:varchar(20);default:0;comment:观看时长"`
}

// 增加播放记录
func (p *PlayRecord) AddPlayRecord(db *gorm.DB) error {
	return db.Create(&p).Error
}

func (p *PlayRecord) GetEpisodePlayRecordByUserId(db *gorm.DB, userId int) (list []PlayRecord, err error) {

	err = db.Where("user_id = ?", userId).Order("created_at desc").Find(&list).Error

	return list, err
}

func (p *PlayRecord) DelPlayRecordByUserId(db *gorm.DB, userId int) error {

	return db.Where("user_id = ?", userId).Delete(&p).Error
}

// 查询视频集数存在
func (e *Episode) ExistEpisode(db *gorm.DB, videoId int,
	episodeNo int) (exist bool, err error) {

	var count int64
	err = db.Where("video_id = ?", videoId).
		Where("episode_no = ?", episodeNo).Count(&count).Error
	return count > 0, err
}

// 剧集视频关联表
type Correlated struct {
	gorm.Model
	EpisodeId int64  //电视剧ID
	VideoId   int64  //分集ID
	NumNo     string //第几集
}

func (u *Correlated) CreateCorrelated(db *gorm.DB) error {
	return db.Create(&u).Error
}
